﻿using EFModel;
using IServices;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xm.Controllers;
using Y2.Dao;

namespace Serives
{
    public class Teengage_major_release : Iengage_major_release
    {
        private readonly MyDBContext he;

        public Teengage_major_release(MyDBContext he)
        {
            this.he = he;
        }
        //三级联动查询
        public Task<string> Dg()
        {
            return Task.Run(() =>
            {
                List<config_file_first_kind> list = he.config_file_first_kind.ToList();
                List<Typn> list2 = CreateTree1(list, 0);
                return JsonConvert.SerializeObject(list2);
            });
        }
        public List<Typn> CreateTree1(List<config_file_first_kind> list, int pid)
        {
            List<Typn> list2 = new List<Typn>();
            List<config_file_second_kind> list1 = he.config_file_second_kind.ToList();
            foreach (config_file_first_kind item in list)
            {
                Typn t = new Typn()
                {
                    value = item.ffk_id,
                    label = item.first_kind_name,
                    children = CreateTree2(list1, item.ffk_id)
                };
                list2.Add(t);
            }
            return list2;
        }
        public List<Typn> CreateTree2(List<config_file_second_kind> list, int pid)
        {
            List<Typn> list2 = new List<Typn>();
            List<config_file_third_kind> lis = he.config_file_third_kind.ToList();
            var list1 = he.config_file_second_kind;
            foreach (var item in list)
            {
                if (item.first_kind_id == pid)
                {
                    Typn t = new Typn()
                    {
                        value = item.fsk_id,
                        label = item.second_kind_name,
                        children = CreateTree3(lis, item.fsk_id)
                    };
                    list2.Add(t);
                }
            }
            return list2;
        }
        public List<Typn> CreateTree3(List<config_file_third_kind> lis, int pid)
        {
            List<Typn> list2 = new List<Typn>();
            var list1 = he.config_file_third_kind;
            foreach (var item in list1)
            {
                if (item.second_kind_id == pid)
                {
                    Typn t = new Typn()
                    {
                        value = item.ftk_id,
                        label = item.third_kind_name
                    };
                    list2.Add(t);
                }
            }
            return list2;
        }
        //二级联动查询
        public Task<string> Dgto()
        {
            return Task.Run(() =>
            {
                List<Typn> list = new List<Typn>();
                var io = he.config_major_kind;
                foreach (var item in io)
                {
                    Typn t = new Typn()
                    {
                        value = item.mfk_id,
                        label = item.major_kind_name,
                        children = he.config_major.Where(e => e.major_kind_id == item.mfk_id).Select(s => new { value = s.mak_id, label = s.major_name })
                    };
                    list.Add(t);
                }
                return JsonConvert.SerializeObject(list);
            });
        }
        //职位发布表添加
        public Task<int> Insert(engage_major_release release)
        {
            return Task.Run(() =>
            {
                var ite = he.engage_major_release.Add(release);
                int num = he.SaveChanges();
                return num;
            });
        }
        //职位发布变更查询
        public Task<ResultInfo> Quer(int currentPage, int pageSize)
        {
            return Task.Run(() =>
            {
                var con = he.engage_major_release;
                var list = he.engage_major_release
                    .Select(e => new { e.mre_id, e.first_kind_id, e.first_kind_name, e.second_kind_id, e.second_kind_name, e.third_kind_id, e.third_kind_name, e.major_kind_id, e.major_kind_name, e.major_id, e.major_name, e.human_amount, e.engage_type, e.deadline, e.register, e.changer, e.regist_time, e.change_time, e.major_describe, e.engage_required })
                    .OrderBy(q => q.mre_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //职位发布修改
        public Task<int> Update(engage_major_release release)
        {
            return Task.Run(() =>
            {
                engage_major_release ite = he.engage_major_release.Find(release.mre_id);
                ite.first_kind_id = release.first_kind_id;
                ite.first_kind_name = release.first_kind_name;
                ite.second_kind_id = release.second_kind_id;
                ite.second_kind_name = release.second_kind_name;
                ite.third_kind_id = release.third_kind_id;
                ite.third_kind_name = release.third_kind_name;
                ite.major_kind_id = release.major_kind_id;
                ite.major_kind_name = release.major_kind_name;
                ite.major_id = release.major_id;
                ite.major_name = release.major_name;
                ite.human_amount = release.human_amount;
                ite.engage_type = release.engage_type;
                ite.deadline = release.deadline;
                ite.changer = release.changer;
                ite.change_time = release.change_time;
                ite.major_describe = release.major_describe;
                ite.engage_required = release.engage_required;
                int num = he.SaveChanges();
                return num;
            });
        }
        //职位发布删除
        public Task<int> Delt(engage_major_release release)
        {
            return Task.Run(() =>
            {
                engage_major_release nv = he.engage_major_release.Find(release.mre_id);
                he.engage_major_release.Remove(nv);
                int num = he.SaveChanges();
                return num;
            });
        }

    }
}
